﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AdminModule.IndexMarkServiceReference;
using AdminModule.Views;
using InpValidator;
using AdminModule.StructEditStratagies;
using AdminModule.CommonClasses;
using System.Collections.ObjectModel;
using AdminModule.ViewModels;

namespace AdminModule.UserControls.StructView
{
    public partial class GradationsStruct : StructViewBase
    {
        private IndexMarkServiceClient client = new IndexMarkServiceClient();

        public GradationsStruct()
        {
            InitializeComponent();
            this.viewModel = this.Resources["commonVM"] as CommonVM2;

            client.SaveAdmGradationHierarhialCompleted += (s, ev) =>
                {
                    base.events.RaiseFreeEvent(FreeEvent.Save, s, ev);
                };
            client.CopyGradationStructElementCompleted += (s, ev) =>
                {
                    base.events.RaiseFreeEvent(FreeEvent.Copy, s, ev);
                };
            client.GetGradationsStructOnDemandCompleted += (s, ev) =>
                {
                    (TreeView.SelectedItem as DTOChilds).Childs.LoadFrom(ev.Result);
                    base.events.RaiseFreeEvent(FreeEvent.LoadData, s, ev); 
                };
        }

        public override void Init(int IDTerm)
        {
            base.Init(IDTerm);
            this.viewModel.Init(StructName.Gradations, IDTerm);
            this.structEditStrategy = new GradationsEditStrategy(IDTerm);
            this.SetBinding(this.viewModel, "Gradations");
            this.SetTemplate(Resources["GragationsRootLevel"] as DataTemplate);
        }

        public override void LoadData()
        {
            this.viewModel.LoadGradationsStruct(IDTerm, base.isLoadOnDemand);
        }

        public override void LoadOnDemand(TreeViewItem treeViewItem, DTOBase obj)
        {
            if (obj is AdmGradation)
            {
                base.events.RaiseBusyEvent(BusyEvent.LoadData);
                client.GetGradationsStructOnDemandAsync(IDTerm, obj as AdmGradation);
            }
        }

        public override void Save()
        {
            base.events.RaiseBusyEvent(BusyEvent.Save);
            client.SaveAdmGradationHierarhialAsync(StructViewBase.changedElements);
        }

        public override void CopySelected(DTOBase targetObject, int toIDTerm)
        {
            base.events.RaiseBusyEvent(BusyEvent.Copy);
            client.CopyGradationStructElementAsync(CurrentItem.selectedItem, toIDTerm, 
                (targetObject is RootTreeView) ? null : targetObject);
        }

        private void TextBlock_Loaded(object sender, RoutedEventArgs e)
        {
            this.TreeView.ExpandToDepth(1);
        }

        public override List<TypeInfo> GetTypesList()
        {
            List<TypeInfo> typesList = new List<TypeInfo>();
            typesList.Add(new TypeInfo 
                { 
                    Name = "Градации", 
                    window = new SelectAddItemsWindow(EnumSelectObjects.AdmGradation),
                    forParentTypesAdd = new List<Type> { typeof(RootTreeView), typeof(AdmGradation) },
                    forParentTypesReplace = new List<Type> { typeof(AdmGradation) }
                });
            typesList.Add(new TypeInfo
            {
                Name = "Градации из группы",
                window = new SelectAddItemsWindow(EnumSelectObjects.AdmGradationsStructSelectGroup, IDTerm, false),
                forParentTypesAdd = new List<Type> { typeof(RootTreeView), typeof(AdmGradation) }
            });
            return typesList;
        }

        private void InitValidator(object sender, RoutedEventArgs e)
        {
            ValidatorBase v = sender as ValidatorBase;
            v.SetEnvironment(LayoutRoot, MainManager);
        }

        private void unitComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            //if (comboBox.SelectedItem != null) return;
            AdmUnit unit = (comboBox.Tag as AdmGradation).Unit;
            // Не у всех корневых градаций есть единица измерения
            if (unit == null) return;

            comboBox.SelectedItem = viewModel.Units.First(f => f != null && f.IDUnit == unit.IDUnit);
        }

        private void unitComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0 || e.AddedItems[0] == null) return;
            ComboBox comboBox = sender as ComboBox;
            AdmUnit unit = comboBox.SelectedItem as AdmUnit;
            if (unit == null) return;
            (comboBox.Tag as AdmGradation).Unit = unit;
        }
    }
}
